{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:40:28.577480Z",
     "start_time": "2023-07-26T17:40:28.524400Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:40:29.773085Z",
     "start_time": "2023-07-26T17:40:28.531132Z"
    }
   },
   "outputs": [],
   "source": [
    "# add path\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "import os\n",
    "# print(os.cwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/shibo/.conda/envs/rap/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from reasoners.visualization import visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:44:32.732062Z",
     "start_time": "2023-07-26T17:44:32.477654Z"
    }
   },
   "outputs": [],
   "source": [
    "from reasoners.visualization.tree_snapshot import NodeData\n",
    "from reasoners.algorithm.mcts import MCTSNode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "mcts_result = pickle.load(open('/home/shibo/LLM-search/logs/gsm8k_MCTS/07152023-044620/algo_output/1.pkl', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SubResult(sub_question='How many eggs does she lay per day?', sub_answer='She lays 16 eggs per day. The answer is 16.', confidence=1.0),\n",
       " SubResult(sub_question='How many does she sell at the farmer’s market?', sub_answer=\"She sells the remaining at the farmer’s market daily for $2 per fresh duck egg. Thus, she sells 16 - 4 = 12 eggs per day at the farmers' market. The answer is 12.\", confidence=1.0),\n",
       " SubResult(sub_question=\"Now we can answer the question: How much does Janet make at the farmers' market every day?\", sub_answer=\"She makes $2 per fresh duck egg. Thus, she makes $2 * 12 = $24 dollars per day at the farmers' market. The answer is $24.\", confidence=1.0)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mcts_result.terminal_state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gsm_node_data_factory(x: MCTSNode):\n",
    "    if not x.state:\n",
    "        return {}\n",
    "    return {\"question\": x.state[-1].sub_question, \"answer\": x.state[-1].sub_answer}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Visualizer URL: https://www.llm-reasoners.net/visualizer/e665d71c-b5dc-40f4-b77b-a538f4ed049f?accessKey=65475fd0\n"
     ]
    }
   ],
   "source": [
    "visualize(mcts_result, node_data_factory=gsm_node_data_factory)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rap",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
